const { ccclass, property } = cc._decorator;
import ImageLoader from './ImageLoader';

@ccclass
export default class Seat extends cc.Component {

    /**头像控制脚本 */
    sprIcon: ImageLoader = null;

    /**玩家名字的文字节点 */
    lblName: cc.Label = null;

    /**玩家分数的文字节点 */
    lblScore: cc.Label = null;

    /**玩家的语音显示容器 */
    voicemsg: cc.Node = null;

    /**玩家的选牌中...已选定...的容器 */
    xuanpai: cc.Node = null;

    /**用户ID */
    userId;

    /**离线图片的节点 */
    offline: cc.Node = null;


    /**准备图片的节点 */
    ready: cc.Node = null;

    /**庄图片的节点 */
    zhuang: cc.Node = null;

    /**分数背景的节点 */
    scoreBg: cc.Node = null;

    /**大赢家图片的节点 */
    dayingjia: cc.Node = null;

    /**聊天的信息框容器 */
    chatBubble: cc.Node = null;

    /**emoji图片的节点 */
    emoji: cc.Node = null;

    /**用户的名字 */
    userName = '';

    /**用户的得分 */
    score = 0;

    /**是否显示大赢家图片 */
    isDayingjia = false;

    /**是否显示离线图片 */
    isOffline = false;

    /**是否显示准备图片 */
    isReady = false;

    /**是否显示庄图片 */
    isZhuang = false;

    /**文字或emoji的剩余显示时间 */
    lastChatTime = 0;

    onLoad() {
        if (!cc.vv) {
            return;
        }

        this.sprIcon = this.node.getChildByName('icon').getComponent(ImageLoader);
        this.lblName = this.node.getChildByName('name').getComponent(cc.Label);
        this.lblScore = this.node.getChildByName('score').getComponent(cc.Label);
        this.voicemsg = this.node.getChildByName('voicemsg');
        this.xuanpai = this.node.getChildByName('xuanpai');
        this.refreshXuanPaiState();

        // 隐藏语音显示容器
        if(this.voicemsg){
            this.voicemsg.active = false;
        }

        if(this.sprIcon && this.sprIcon.node.getComponent(cc.Button)){
            cc.vv.utils.addClickEvent(this.sprIcon.node, this.node, 'Seat', 'onIconClicked');
        }

        this.offline = this.node.getChildByName('offline');
        this.ready = this.node.getChildByName('ready');
        this.zhuang = this.node.getChildByName('zhuang');
        this.scoreBg = this.node.getChildByName('Z_money_frame');
        this.dayingjia = this.node.getChildByName('dayingjia');
        this.chatBubble = this.node.getChildByName('ChatBubble');
        if(this.chatBubble){
            this.chatBubble.active = false;
        }
        this.emoji = this.node.getChildByName('emoji');
        if(this.emoji){
            this.emoji.active = false;
        }

        this.refresh();

        if(this.sprIcon && this.userId){
            this.sprIcon.setUserID(this.userId);
        }
    }


    /**初始化选牌中...或已选定....的显示 */
    refreshXuanPaiState(){
        if(!this.xuanpai){
            return;
        }

        this.xuanpai.active = cc.vv.gameNetMgr.isHuanSanZhang;
        if(!cc.vv.gameNetMgr.isHuanSanZhang){
            return;
        }

        this.xuanpai.getChildByName('xz').active = false;
        this.xuanpai.getChildByName('xd').active = false;

        const seat = cc.vv.gameNetMgr.getSeatByID(this.userId);
        if(seat){
            if (!seat.huanPais){
                this.xuanpai.getChildByName('xz').active = true;
            }
            else{
                this.xuanpai.getChildByName('xd').active = true;
            }
        }
    }


    /**头像点击事件 */
    onIconClicked(){

    }


    /**更新用户状态 */
    refresh(){
        if(this.lblName){
            this.lblName.string = this.userName;
        }
        if(this.lblScore){
            this.lblScore.string = this.score.toString();
        }
        if(this.dayingjia){
            this.dayingjia.active = this.isDayingjia;
        }
        if(this.offline){
            this.offline.active = this.isOffline && !this.userName;
        }
        if(this.ready){
            this.ready.active = this.isReady && (cc.vv.gameNetMgr.numOfGames > 0);
        }
        if(this.zhuang){
            this.zhuang.active = this.isZhuang;
        }
        this.node.active = !!this.userName;
    }

    
    /**
     * 更新用户名、分数、大赢家的显示
     * @param name 玩家的名字
     * @param score 玩家的分数
     * @param isDayingjia 是否是大赢家 
     */
    setInfo(name: string, score: number, isDayingjia?: boolean){
        this.userName = name;
        this.score = score;
        if(this.score == null){
            this.score = 0;
        }
        this.isDayingjia = isDayingjia;
        if(this.scoreBg){
            this.scoreBg.active = this.score != null;
        }
        if(this.lblScore){
            this.lblScore.node.active = this.score != null;
        }
        this.refresh();
    }


    /**更新庄的显示状态 */
    setZhuang(value: boolean){
        this.isZhuang = value;
        this.zhuang && (this.zhuang.active = value);
    }

    /**更新准备的显示状态 */
    setReady(isReady: boolean){
        this.isReady = isReady;
        if(this.ready){
            this.ready.active = this.isReady && (cc.vv.gameNetMgr.numOfGames > 0);
        }
    }

    /**更新用户ID */
    setID(id){
        const idNode = this.node.getChildByName('id');
        if(idNode){
            const lbl = idNode.getComponent(cc.Label);
            lbl.string = 'ID:' + id;
        }

        this.userId = id;
        if(this.sprIcon){
            this.sprIcon.setUserID(id);
        }
    }

    /**设置离线图片的显示 */
    setOffline(isOffline: boolean){
        this.isOffline = isOffline;
        if(this.offline){
            this.offline.active = this.isOffline && this.userId != '';
        }
    }

    /**设置聊天栏的内容及显示 */
    setChat(content: string){
        if(!this.chatBubble || !this.emoji){
            return;
        }
        this.emoji.active = false;
        this.chatBubble.active = true;
        this.chatBubble.getComponent(cc.Label).string = content;
        this.chatBubble.getChildByName('New Label').getComponent(cc.Label).string = content;
        this.lastChatTime = 3;
    }

    /**更新emoji */
    setEmoji(emoji){
        if(!this.emoji){
            return;
        }
        this.chatBubble.active = false;
        this.emoji.active = true;
        this.emoji.getComponent(cc.Animation).play(emoji);
        this.lastChatTime = 3;
    }

    /**更新语音节点的显示 */
    setVoicemsg(show: boolean){
        if(this.voicemsg){
            this.voicemsg.active = show;
        }
    }

    update(dt){
        if(this.lastChatTime > 0){
            this.lastChatTime -= dt;
            if(this.lastChatTime < 0){
                this.chatBubble.active = false;
                this.emoji.active = false;
                this.emoji.getComponent(cc.Animation).stop();
            }
        }
    }
}
